package exocr.exocrengine;

import android.content.Context;

import com.yyfax.common.CLog;
import com.yyfax.ph.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/***
 * @version 扫描银行卡和身份证管理类，初始化识别核心和字典文件
 * @创建日期 2016/11/3
 * @作者 xiongyufeng
 * @描述 DictManager
 */
public class DictManager {
	private static final String TAG = DictManager.class.getSimpleName();

	private static DictManager mDictManager;
	private DictInitListener mDictInitListener;

	public static DictManager getInstance() {
		if (null == mDictManager) {
			mDictManager = new DictManager();
		}
		return mDictManager;
	}

	/**
	 * 初始化成功标志
	 */
	private boolean hasInit = false;

	public boolean hasInit() {
		return hasInit;
	}

	/**
	 * 初始化扫描银行卡和身份证
	 */
	public void initDict(Context mContext, DictInitListener dictInitListener) {
		this.mDictInitListener = dictInitListener;
		byte dpPath[] = new byte[256];
		Context context = mContext.getApplicationContext();
		String dictPath = context.getFilesDir().getAbsolutePath();
		//检查是否有zocr0文件，如果没有要从ASSES中拷贝
		if (!checkExist(dictPath + "/zocr0.lib")) {
			File destDir = new File(dictPath);
			boolean status = destDir.exists();
			if (!status) {
				status = destDir.mkdirs();
			}

			if (status) {
				boolean a = copyFile(context, "zocr0.lib", dictPath + "/zocr0.lib");
				if (!a) {
					if (null != this.mDictInitListener) {
						this.mDictInitListener.onDictInitFailure(mContext.getString(R.string.init_dict_failure_tip_of_no_dict_strs));
					}
					return;
				}
			}
		}

		String filePath = dictPath;

		for (int i = 0; i < filePath.length(); i++)
			dpPath[i] = (byte) filePath.charAt(i);
		dpPath[filePath.length()] = 0;
		/**
		 * 初始化关键JNI调用
		 */
		int result = EXOCREngine.nativeInit(dpPath);
		CLog.i(TAG, ">>>>" + result);
		if (result < 0) {
			hasInit = false;
			if (null != this.mDictInitListener) {
				this.mDictInitListener.onDictInitFailure(mContext.getString(R.string.init_dict_failure_tip_of_expired_strs));
			}
			return;
		} else {
			hasInit = true;
			if (null != this.mDictInitListener) {
				this.mDictInitListener.onDictInitSuccess();
			}
		}
		/**
		 * 签名授权
		 */
		EXOCREngine.nativeCheckSignature(context);
	}

	/**
	 * 释放相关资源文件
	 */
	public void finishDict() {
		hasInit = false;
		/**
		 * 销毁字典关键JNI调用
		 */
		EXOCREngine.nativeDone();
	}

	/**
	 * 从Assets目录中拷贝zocr0.lib到指定目录
	 *
	 * @param context
	 * @param from
	 * @param to
	 * @return
	 */
	private boolean copyFile(Context context, String from, String to) {
		boolean ret;
		try {
			int byteSum = 0;
			int byteRead = 0;

			InputStream inStream = context.getResources().getAssets().open(from);
			File file = new File(to);
			OutputStream fs = new FileOutputStream(file);
			byte[] buffer = new byte[1024];
			while ((byteRead = inStream.read(buffer)) != -1) {
				byteSum += byteRead;
				fs.write(buffer, 0, byteRead);
			}
			inStream.close();
			fs.close();
			ret = true;

		} catch (Exception e) {
			ret = false;
		}
		return ret;
	}

	private boolean checkExist(String filepath) {
		File file = new File(filepath);
		if (file.exists()) {
			return true;
		}
		return false;
	}

	public interface DictInitListener {
		void onDictInitSuccess();

		void onDictInitFailure(String failureTips);
	}

}
